package com.mingmen.canting.caigourenyuan;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.CaiWanModel;
import com.mingmen.canting.model.JingLiRukuModel;

import java.util.List;


/**
 * @author qdafengzi
 */
public class CaiGouWanRightAdapter extends RecyclerView.Adapter<CaiGouWanRightAdapter.ViewHolder> {
    private Context mContext;
    private List<CaiWanModel.DataBean.DetailsBean> mList;

    private int mPosition;

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int position, List<CaiWanModel.DataBean.DetailsBean> mList);
    }

    private OnitemClick onitemClick;   //定义点击事件接口

    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    public CaiGouWanRightAdapter(Context context, List<CaiWanModel.DataBean.DetailsBean> list, int position) {
        mContext = context;
        mList = list;
        mPosition = position;
    }

    /**
     * 新增方法
     *
     * @param position 位置
     */
    public void setPosition(int position) {
        this.mPosition = position;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jingli_right, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CaiWanModel.DataBean.DetailsBean detailsBean = mList.get(position);
        holder.lay_option.setTag(position);

        holder.commodityName.setText(mList.get(position).getCommodityName());
        holder.spec.setText("规格: " + detailsBean.getSpecs());
        holder.applyAmount.setText("数量:" + mList.get(position).getApplyAmount() + "");
        Glide.with(mContext).load(AppConfig.IMGURL + detailsBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);
        holder.arrivePrice.setText("实价: " + mList.get(position).getArrivePrice() + "元");
        holder.layout_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onitemClick != null) {
                    onitemClick.onItemClick(v,position, mList);
                }
            }
        });


     /*   holder.commodityName.setText(mList.get(position).getCommodityName());

        holder.applyAmount.setText("数量:" + mList.get(position).getApplyAmount() + "");
        holder.text_mark.setText(mList.get(position).getMark());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onitemClick != null) {
                    onitemClick.onItemClick(position, mList);
                }

            }
        });*/

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout lay_option;
        TextView tvOption;
        TextView commodityName;
        TextView applyAmount;

        ImageView ivAvatar;
        TextView spec;

        TextView arrivePrice;
        LinearLayout layout_jump;

        ViewHolder(View view) {
            super(view);

            lay_option = view.findViewById(R.id.lay_option);
            tvOption = view.findViewById(R.id.tv_option);
            commodityName = view.findViewById(R.id.commodityName);
            applyAmount = view.findViewById(R.id.applyAmount);

            ivAvatar = view.findViewById(R.id.ivAvatar);
            spec = view.findViewById(R.id.spec);
            arrivePrice = view.findViewById(R.id.arrivePrice);
            layout_jump = view.findViewById(R.id.layout_jump);
        }
    }
}